package com.chunuo.eduservice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chunuo.commonutils.R;
import com.chunuo.eduservice.entity.EduTeacher;
import com.chunuo.eduservice.entity.vo.TeacherQuery;
import com.chunuo.eduservice.service.EduTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-04
 */
@RestController
@RequestMapping("/eduservice/edu-teacher")
@Slf4j
//@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService eduTeacherService;

    @GetMapping("/list")
    public R findAll(){
//        try {
//            int a = 1/0;
//        } catch (Exception e) {
//            throw new GuliException(500,"by/....自定义异常");
//        }
        List<EduTeacher> list = eduTeacherService.list(null);
        return R.ok().data("data",list);
    }

    @GetMapping("/info/{id}")
    public R findById(@PathVariable("id")String id){
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("data",eduTeacher);
    }

    @PostMapping("/pageList/{page}/{limit}")
    public R pageList(@RequestBody TeacherQuery teacherQuery,
                      @PathVariable("page") Integer page,
                      @PathVariable("limit") Integer limit) {

        IPage<EduTeacher> pageList = eduTeacherService.pageList(page,limit,teacherQuery);
        return R.ok().
                data("data",pageList.getRecords()).
                data("page",pageList.getCurrent()).
                data("total",pageList.getTotal());
    }

    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher){
        eduTeacherService.save(eduTeacher);
        return R.ok();
    }

    @PutMapping("/update")
    public R update(@RequestBody EduTeacher eduTeacher){
        eduTeacherService.updateById(eduTeacher);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id")String id){
        eduTeacherService.removeById(id);
        return R.ok();
    }

}

